package com.atstudying.mall.ware.service.impl;

import com.atstudying.common.constant.WareConstant;
import com.atstudying.common.utils.PageUtils;
import com.atstudying.common.utils.Query;
import com.atstudying.mall.ware.dao.PurchaseDao;
import com.atstudying.mall.ware.entity.PurchaseDetailEntity;
import com.atstudying.mall.ware.entity.PurchaseEntity;
import com.atstudying.mall.ware.service.PurchaseDetailService;
import com.atstudying.mall.ware.service.PurchaseService;
import com.atstudying.mall.ware.vo.MergeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    PurchaseDetailService purchaseDetailService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceiveList(Map<String, Object> params) {

        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();

        //查询出1,0的数据为status
        wrapper.eq("status",0).or().eq("status",1);
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),wrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void merge(MergeVo mergeVo) {

        Long purchaseId = mergeVo.getPurchaseId();
        if (purchaseId==null){
            PurchaseEntity purchaseEntity=new PurchaseEntity();

            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATE.getCode());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            Long purchaseEntityId = purchaseEntity.getId();
        }

        //todo 却采购单状态是0或1才可以
        if (WareConstant.PurchaseStatusEnum.CREATE.getCode()==0 ||
                WareConstant.PurchaseStatusEnum.ASSIGEND.getCode()==1){
            List<Long> items=mergeVo.getItems();
            Long finalPurchaseEntityId=purchaseId;

            List<PurchaseDetailEntity> collect = items.stream().map(i -> {
                PurchaseDetailEntity purchaseEntity = new PurchaseDetailEntity();
                purchaseEntity.setStatus(WareConstant.PurchaseDetailsStatusEnum.ASSIGEND.getCode());
                purchaseEntity.setPurchaseId(finalPurchaseEntityId);
                purchaseEntity.setId(i);
                return purchaseEntity;
            }).collect(Collectors.toList());

            purchaseDetailService.updateBatchById(collect);
            PurchaseEntity purchaseEntity=new PurchaseEntity();
            purchaseEntity.setId(purchaseId);
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.updateById(purchaseEntity);
        }

    }

    /**
     *
     * @param ids 采购单id
     */
    @Override
    public void received(List<Long> ids) {

        //1：确认当前采购单是新建或者已分配状态
        //把采购单的详细信息查出来
        List<PurchaseEntity> purchaseEntities = ids.stream().map(id -> {
            PurchaseEntity byId = this.getById(id);
            return byId;
        }).filter(item->{
            if (item.getStatus()==WareConstant.PurchaseStatusEnum.CREATE.getCode()||
                    item.getStatus()==WareConstant.PurchaseStatusEnum.ASSIGEND.getCode()  ){
                return true;
            }
            return false;
        }).map(items->{
            items.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            items.setUpdateTime(new Date());

            return items;
        }).collect(Collectors.toList());

        //2：改变采购单的状态
        this.updateBatchById(purchaseEntities);

        //3：改变采购项的状态
        purchaseEntities.forEach((item->{
            List<PurchaseDetailEntity> entities= purchaseDetailService.listDetailByPurchaseId(item.getId());

            List<PurchaseDetailEntity> purchaseDetailEntities = entities.stream().map(entitiess -> {
                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();

                purchaseDetailEntity.setId(entitiess.getId());
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailsStatusEnum.BUYING.getCode());
                return purchaseDetailEntity;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(purchaseDetailEntities);
        }));


    }

}